home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / kelv.zip / KELV.DOC < prev    next >
Lisp/Scheme  |  1990-11-13  |  21KB  |  475 lines

  1. The Kelvinator  - AutoLisp encryption utility.
  2. Release 2a: The Kelvinator Strikes Back
  3. by Kelvin R. Throop
  4. September 2nd, 1987
  5.  
  6. (C) Copyright 1986,1987 Autodesk, Inc.
  7.  
  8. License is granted for the use of this software and documentation on a no 
  9. charge, sharing basis. You may distribute and copy this software and 
  10. documentation freely as long as it remains a complete unaltered package and 
  11. is not sold for profit.  
  12.  
  13.  
  14. ------------------------
  15.  
  16. Following the release of AutoCAD version 2.1, independent developers began 
  17. to create complex applications employing the AutoCAD menu macro language 
  18. and the Variables and Expressions feature as an embedded programming 
  19. language.  With the release of version 2.18, which introduced the AutoLisp 
  20. programming language, AutoCAD became a fully programmable and extensible 
  21. system. 
  22.  
  23. From inception, AutoCAD's open architecture has been intended to encourage 
  24. outside developers and individual users to customize AutoCAD and implement 
  25. applications for diverse markets.  Since menu macros and AutoLisp are 
  26. interpreted languages, applications must be shipped in source code form, 
  27. and Autodesk recognized the need to protect the developers of these 
  28. applications from theft of their proprietary products. 
  29.  
  30. Encryption of menu macros and AutoLisp programs was implemented and made 
  31. available to authorized developers.  This encryption never purported to be 
  32. particularly secure, but it does prevent casual examination of distributed 
  33. code. Given that a standard AutoCAD must be able to decode a file encrypted 
  34. by any developer, there is no cryptological technique of which I am aware 
  35. which would be significantly more secure than the one used.  The fact that 
  36. the encryption program is made available to hundreds of developers who 
  37. cannot always prevent its further distribution makes penetration by reverse 
  38. engineering of the program or a known-plaintext attack inevitable should 
  39. somebody be so callous as to steal the work of our developers and undermine 
  40. the viability of their businesses. 
  41.  
  42.  
  43. A SECOND LAYER OF PROTECTION
  44.  
  45. Since encrypting files cannot provide total security, a second level of 
  46. defence was called for.  The Kelvinator is a program which translates an 
  47. AutoLisp program into a form which is essentially gibberish when examined 
  48. by a human but which should execute identically to the original program.  
  49. Thus, if the distributed program is decrypted, the resulting source code 
  50. will be little use to the looter. 
  51.  
  52. Kelvination of a program consists of:
  53.  
  54. 1) Deleting comments.
  55. 2) Translating variable names into gibberish.
  56. 3) Removing indentation and line breaks.
  57.  
  58. As an example of the consequences of Kelvination, the following AutoLisp 
  59. program was processed. 
  60.  
  61. ;
  62. ;       Display spiral
  63. ;
  64. ;       Designed and implemented by
  65. ;       Kelvin R. Throop in January 1985
  66. ;
  67. ;       (cspiral <# rotations> <base point>
  68. ;                <growth per rotation>
  69. ;                <points per circle>)
  70. ;
  71. (defun cspiral (ntimes bpoint cfac lppass
  72.         / ang dist tp ainc dinc circle bs cs)
  73.         (setq cs (getvar "cmdecho"))
  74.         (setq bs (getvar "blipmode"))
  75.         (setvar "blipmode" 0)
  76.         (setvar "cmdecho" 0)
  77.         (setq circle (* 3.141596235 2))
  78.         (setq ainc (/ circle lppass))
  79.         (setq dinc (/ cfac lppass))
  80.         (setq ang 0.0)
  81.         (setq dist 0.0)
  82.         (command "pline" bpoint)
  83.         (repeat ntimes
  84.            (repeat lppass
  85.               (setq tp (polar bpoint
  86.                (setq ang (+ ang ainc))
  87.                (setq dist (+ dist dinc))))
  88.               (command tp)
  89.            )
  90.         )
  91.         (command)
  92.         (setvar "blipmode" bs)
  93.         (setvar "cmdecho" cs)
  94.         nil
  95. )
  96. ;
  97. ;       Interactive spiral generation
  98. ;
  99. (defun C:SPIRAL ( / nt bp cf lp)
  100.         (prompt "\nCentre point: ")
  101.         (setq bp (getpoint))
  102.         (prompt "\nNumber of rotations: ")
  103.         (setq nt (getint))
  104.         (prompt "\nGrowth per rotation: ")
  105.         (setq cf (getdist bp))
  106.         (prompt "\nPoints per rotation: ")
  107.         (setq lp (getint))
  108.         (cond ((null lp) (setq lp 30)))
  109.         (cspiral nt bp cf lp)
  110. )
  111.  
  112. The output of the Kelvinator is as follows (line breaks have been manually
  113. inserted: there were none in the actual output file).
  114.  
  115. (DEFUN Qj(Q@ QQ Ql Q& / Q1 Q# Q0 Q$ QO
  116. Q| Q% Q?j)(SETQ
  117. Q?j(GETVAR"cmdecho"))(SETQ
  118. Q%(GETVAR"blipmode"))
  119. (SETVAR"blipmode"0)(SETVAR"cmdecho"0)(SETQ
  120. Q|(* 3.141596235 2))(SETQ Q$(/ Q|
  121. Q&))(SETQ QO(/ Ql Q&))(SETQ Q1 0.0)(SETQ
  122. Q# 0.0)(COMMAND"pline"QQ)(REPEAT
  123. Q@(REPEAT Q&(SETQ Q0(POLAR QQ(SETQ Q1(+
  124. Q1 Q$))(SETQ Q#(+ Q# QO))))(COMMAND
  125. Q0)))(COMMAND)(SETVAR"blipmode"Q%)
  126. (SETVAR"cmdecho"Q?j)NIL)(DEFUN
  127. C:SPIRAL(/ Qjj Q@j QQj
  128. Qlj)(PROMPT"\nCentre point: ")(SETQ
  129. Q@j(GETPOINT))(PROMPT
  130. "\nNumber of rotations: " )(SETQ
  131. Qjj(GETINT))(PROMPT"\nGrowth per
  132. rotation: ")(SETQ QQj(GETDIST
  133. Q@j))(PROMPT"\nPoints per rotation: ")
  134. (SETQ Qlj(GETINT))(COND((NULL
  135. Qlj)(SETQ Qlj 30)))(Qj Qjj Q@j QQj Qlj))
  136.  
  137.  
  138. If you imagine several thousand lines of this in a real application instead 
  139. of the few lines generated by this simple AutoLisp program (which, by the 
  140. way, was the first AutoLisp program ever written), it should be clear that 
  141. not much can be gleaned from decrypting such ``source code''. 
  142.  
  143.  
  144. Kelvinating a File
  145.  
  146. To Kelvinate a file, use the command:
  147.  
  148. KELV file [options]
  149.  
  150. Input is read from the specified file.  If no file type is specified, .lsp 
  151. is assumed.  Output is written to standard output, and will usually be 
  152. redirected to a file.  Various options can be specified to control the 
  153. operation of the Kelvinator; these are discussed below.  For example, to 
  154. Kelvinate the AutoLisp application brainsim.lsp and write the output into  
  155. ubrains.lsp, use the command: 
  156.  
  157. KELV brainsim >ubrains.lsp
  158.  
  159. Note that a while a file type of .lsp is assumed for the input file, an 
  160. explicit file type must be supplied when the output of the Kelvinator is 
  161. redirected. 
  162.  
  163.  
  164. The Definition File
  165.  
  166. When a program is Kelvinated, all of the user-defined symbols are 
  167. translated to gibberish, but AutoLisp's built-in symbols (such as CONS, 
  168. SETQ, and ENTGET) must not be modified.  In addition the application may 
  169. wish to leave some symbols unchanged (for example the C:name used to invoke 
  170. user-defined commands, or functions called by the user directly from the 
  171. keyboard). Finally, when a multiple-file program is Kelvinated, consistent 
  172. translation must be applied across all files comprising the program.  All 
  173. of these requirements are implemented via the Kelvinator definition file. 
  174.  
  175. If you are simply using the Kelvinator to protect standard AutoLisp 
  176. programs, you probably won't have any need to examine or modify the 
  177. definition file. Feel free to skip this section until you need it. 
  178.  
  179. When the Kelvinator is called as in the example above, it searches the 
  180. current directory for a file named.  If you wish to use a different 
  181. definition file, multiple definition files, or a different source for the 
  182. definition file, you may use the -d switch on the KELV call line to supply 
  183. a different file name.  For example: 
  184.  
  185. KELV prog1 >kp1.lsp -dmydefs.def
  186. KELV prog2 >kp2.lsp -d/usr/appdev/KELV.def
  187. KELV prog3 >kp3.lsp -dKELV.def -dlocal.def
  188.  
  189.  
  190. In the first example a locally-created definition file is used instead of 
  191. the standard KELV.def.  In the second, the Kelvinator is told to obtain its 
  192. definition file from the specified path rather than from the current 
  193. directory. In the third example, two definition files are used: the 
  194. standard definitions and a local version specifying additional symbols.  
  195. Any number of definition files may be specified when the Kelvinator is 
  196. called; they are loaded in the order named on the call line.  If a symbol 
  197. is declared more than once, whether within one definition file or across 
  198. several, the last declaration takes precedence (this allows local 
  199. definitions to override those given in the standard KELV.def). 
  200.  
  201. A Kelvinator definition file is composed of four types of declarations: 
  202. Protected symbols, Pattern masks, Translation specifications, and a Symbol 
  203. code.  Each type of declaration is placed in a section of the file 
  204. introduced by a section definition statement whose first character is a 
  205. right parenthesis `` )''.  Blank lines and comment lines which begin with 
  206. two right parentheses are ignored. 
  207.  
  208. Let's look at a simple definition file:
  209.  
  210.  
  211. )) Sample Kelvinator definition file
  212.  
  213. )Pattern
  214. C:?
  215.  
  216. )Protect
  217. cons
  218. setq
  219. entget
  220.  
  221. )Translate
  222. mysym z1
  223. yoursym z2
  224.  
  225. )Protect
  226. car
  227.  
  228. )Code
  229. 200
  230.  
  231.  
  232. This file contains five sections: 
  233. one  )Pattern section, two )Protect sections, one  )Translate section, and 
  234. one )Code section.  A file may contain as many sections as you like.  
  235. Capital and lower case letters are considered identical; this is consistent 
  236. with AutoLisp's symbol syntax. 
  237.  
  238. THE )PROTECT SECTION
  239.  
  240. The  )Protect section consists simply of a list of names, one per line.  
  241. Any symbol named in a  )Protect section will be left unchanged by the 
  242. Kelvinator. In this case, the four standard AutoLisp symbols  CONS,  SETQ,  
  243. ENTGET, and  CAR will be left intact when a file is Kelvinated.  The 
  244. standard KELV.def lists all AutoLisp built-in names as protected symbols.  
  245. If a new built-in name is added in a subsequent release of AutoLisp, simply 
  246. adding it to the  KELV.def file will cause it to be correctly processed by 
  247. the Kelvinator. 
  248.  
  249. If your application provides functions the user invokes from the keyboard 
  250. (or from a menu), you may request the Kelvinator to leave these names 
  251. untranslated by adding them to the  )Protect section. The best way to 
  252. accomplish this is to create a second definition file for your application 
  253. and specify both  KELV.def and your file with  -d specifications when 
  254. invoking the Kelvinator. 
  255.  
  256.  
  257. THE )PATTERN SECTION
  258.  
  259. Sometimes an entire syntactic class of symbols should be left untranslated.  
  260. An example of this are the names used to invoke user-defined commands in 
  261. AutoLisp; all user-defined commands are functions whose names begin with
  262. ``C:''.  Each line in the pattern section specifies a pattern against which 
  263. symbols are tested. If a symbol matches the pattern up to the length of the 
  264. pattern, with the ``?'' character in the pattern matching any non-null 
  265. character in the symbol, that symbol is automatically protected (i.e., not 
  266. translated by the Kelvinator). 
  267.  
  268. The standard  KELV.def file contains the pattern declaration `` ?:'', which 
  269. protects all symbols with a colon as their second character; this is 
  270. compatible with the original Kelvinator in which this was hard-coded. 
  271.  
  272.  
  273. THE )TRANSLATE SECTION
  274.  
  275. When the Kelvinator encounters a symbol in a program which is neither 
  276. protected by matching a pattern nor by having been named in a  )Protect 
  277. section, it searches for the symbol in the translation table.  If the 
  278. symbol is found, the value from the translation table replaces the original 
  279. symbol.  If the symbol is not in the translation table, the Kelvinator 
  280. creates a gibberish symbol and makes an entry in the translation table so 
  281. that all subsequent occurrences of the new symbol will be translated to it. 
  282.  
  283. The standard definition file,  KELV.def, contains no  )Translate section, 
  284. so all nonprotected symbols are assigned new gibberish values.  If you wish 
  285. to cause a symbol to be translated to a specific value rather than have the 
  286. Kelvinator make up a gibberish value for it, simply specify the original 
  287. symbol and the value it should be translated to, separated by a single 
  288. blank character, as a line in the  )Translate section.  In the example 
  289. above, all occurrences of `` mysym'' will be translated to `` z1'', and all 
  290. occurrences of `` yoursym'' to `` z2''. 
  291.  
  292. The  )Translate section is rarely used to perform explicit symbol 
  293. translation. The primary application of the translate section is to permit 
  294. convenient Kelvination of multi-file programs, as discussed below. 
  295.  
  296.  
  297. THE )CODE SECTION
  298.  
  299. When the Kelvinator processes a symbol not present in the  Protect or 
  300. Translate sections which does not match a pattern, a gibberish name is 
  301. generated and this and all subsequent occurrences of the symbol are 
  302. translated to the gibberish name.  The gibberish name is generated 
  303. programmatically from a symbol code which is incremented for each new 
  304. symbol encountered.  The Kelvinator normally starts this code at zero, but 
  305. can be instructed to generate symbols starting at any desired code by 
  306. specifying the starting number in a )Code section.  If more than one number 
  307. is specified, in one or more )Code section(s), the last number encountered 
  308. is used.  The  )Code section is almost never specified explicitly; it is 
  309. generated by the Kelvinator to allow automatic Kelvination of multi-file 
  310. programs. 
  311.  
  312.  
  313. MULTIPLE FILE PROGRAMS
  314.  
  315. If an AutoLisp program is a single file which is loaded into AutoCAD and 
  316. run as a unit, the default translation performed by the Kelvinator is 
  317. usually adequate.  If an application consists of more than one original  
  318. .lsp file and the running program loads different AutoLisp modules during 
  319. execution, special care must be taken when Kelvinating the application.  
  320. Simply running the Kelvinator on a source file translates all the variable 
  321. names in it into a standard format.  If each module of a multiple module 
  322. program were Kelvinated, this would result in two distinct problems: 
  323.  
  324.  
  325. 1) Variables shared between modules would be given different names in 
  326. different modules, so values passed would not be received correctly in the 
  327. loaded module. 
  328.  
  329. 2) Variables with distinct names in the original source might be translated 
  330. into the same name in the Kelvinated output.  Thus duplication of variable 
  331. names would almost certainly occur. 
  332.  
  333.  
  334. SHARED VARIABLES
  335.  
  336. The first problem may be addressed in any of several ways.  One option is 
  337. to simply give all variables shared between modules names which match a 
  338. pattern that causes them to be remain untranslated.  Using the standard 
  339. pattern in KELV.def, simply using a colon as the second character of all 
  340. shared variables accomplishes this. 
  341.  
  342. If you don't want to name the shared variables to match a pattern, you can 
  343. simply make a list of all the shared variables and specify that list as a  
  344. )Protect section in a definition file. None of the shared variables will be 
  345. translated, and thus they will retain their values across modules. 
  346.  
  347. Finally, and by far the easiest, you can use the technique for automatic 
  348. multiple file Kelvination given below.  This will guarantee that variables 
  349. with the same name in the original source will share the same encoded name 
  350. in the Kelvinated output. 
  351.  
  352.  
  353. UNIQUE VARIABLE NAMES
  354.  
  355. There are two approaches to preventing inadvertent duplication of variable 
  356. names as a result of Kelvination.  The first technique requires the use of 
  357. an option on the call to the Kelvinator which permits generation of unique 
  358. names for each module.  If the Kelvinator is called with a  -P prefix 
  359. argument, the generated variable names will begin with the specified 
  360. prefix.  If no prefix is specified, the default of `` Q'' is used.  The 
  361. prefix may be any string which is a valid AutoLisp symbol, but should not 
  362. be a symbol likely to duplicate an AutoLisp-defined symbol.  If you're 
  363. Kelvinating a four module electronic application, you might use: 
  364.  
  365.  
  366. KELV arisia   -pZA  >parisia.lsp
  367. KELV scapture -pZB  >pscapt.lsp
  368. KELV pcboard  -pZC  >ppcbd.lsp
  369. KELV simulate -pZD  >psim.lsp
  370.  
  371.  
  372. Symbols in the respective modules will be given prefixes of ZA through  ZD, 
  373. and are therefore guaranteed to be unique. Remember that AutoLisp is far 
  374. more efficient when storing symbols of 6 characters or less, so don't go 
  375. overboard in choosing a variable prefix.  Two characters should be 
  376. adequate. 
  377.  
  378. If you use the automatic multiple file Kelvination technique described 
  379. below, you need not assign unique prefixes; distinct names in the original 
  380. set of files will be guaranteed distinct in the Kelvinated output. 
  381.  
  382.  
  383. AUTOMATIC MULTI-FILE KELVINATION
  384.  
  385. The Kelvinator allows AutoLisp programs which span multiple files to be 
  386. Kelvinated in a close-to-automatic manner.  When the Kelvinator is used in 
  387. this fashion, names will be translated for the program taken as a whole, 
  388. thus preserving the uniqueness of names across modules and guaranteeing 
  389. that two names which were distinct in the original set of files will never 
  390. be translated to the same name. 
  391.  
  392. Automatic multi-file Kelvination is made possible by a feature which causes 
  393. the Kelvinator to write out the translation table used to Kelvinate a file 
  394. in a form that can be used as the definition file for a subsequent run of 
  395. the Kelvinator.  If the  -W file specification is placed on a call to the 
  396. Kelvinator, after a file has been Kelvinated a complete definition file is 
  397. written to the named file.  All protected symbols and patterns used for the 
  398. Kelvination are written to the file, and  )Translate commands are given for 
  399. all symbols changed by the Kelvinator.  In addition, a  )Code section is 
  400. written to start symbol generation at the correct point in the next module.  
  401. By passing on these definition files from run to run of the Kelvinator, the 
  402. translated names assigned by the Kelvinator may be made uniform across the 
  403. input file set. 
  404.  
  405. For example, here are the commands you would use to perform automatic multi-
  406. file Kelvination on the electronic application mentioned in the last 
  407. section: 
  408.  
  409.  
  410. KELV         -wd1.def arisia   >parisia.lsp
  411. KELV -dd1.def -wd2.def scapture >pscapt.lsp
  412. KELV -dd2.def -wd3.def pcboard  >ppcbd.lsp
  413. KELV -dd3.def -wd4.def simulate >psim.lsp
  414.  
  415.  
  416. Note how we pass the output definitions from each file (which include all 
  417. of the information from the files processed before) on to the Kelvination 
  418. of the next file.  We don't specify the  -D switch for the first file since 
  419. we're starting with the standard KELV.def file for that run.  We don't need 
  420. to save the definitions from the last file, but we did in this case because 
  421. the saved definition file can be a handy tool for debugging the 
  422. application.  If a Kelvinated program crashes, the AutoLisp walkback will 
  423. contain mostly gibberish symbols.  If you retain a copy of the final 
  424. definition file from the Kelvination run, you can translate this walkback 
  425. into the original code and more easily locate the problem in the source.  
  426. This capability makes saving the definitions worthwhile even for a single 
  427. file program. 
  428.  
  429.  
  430. OTHER BENEFITS OF KELVINATION
  431.  
  432. Even if security is not a consideration, you might consider Kelvinating 
  433. your program.  By removing comments and white space and replacing long 
  434. descriptive variable names with short cryptic ones, Kelvination may 
  435. radically reduce the size of an AutoLisp program. 
  436.  
  437. For example, Kelvination of AutoShade reduced the  ASHADE.LSP file from 
  438. 13669 bytes to 4095 bytes, a reduction of more than three to one (the code 
  439. was heavily commented).  In addition, if you intend to Kelvinate the file 
  440. before shipment, you are free to use long variable names for documentation 
  441. purposes; Kelvination will reduce them to short names that AutoLisp can 
  442. store efficiently. The new Kelvinator compresses its output somewhat more 
  443. densely than the original Kelvinator---it knows quite a bit more about 
  444. AutoLisp's input parser and sails very close to the wind in eliminating 
  445. spaces wherever AutoLisp doesn't need them to delimit symbols in the input. 
  446.  
  447. The only speed benefit of Kelvination is that gained while loading because 
  448. a Kelvinated file is usually smaller.  Once loaded, a Kelvinated program 
  449. will run in exactly the same time as a non-Kelvinated one (unless 
  450. shortening of variable names has increased free memory and thus reduced 
  451. garbage collections, which will yield a small edge to the Kelvinated 
  452. program). 
  453.  
  454. ------------------------------
  455.  
  456. ;;; Graphic Systems, Inc. provides this program 'as is', without warranty of 
  457. ;;; any kind, either expressed or implied, including, but not limited to the 
  458. ;;; implied warranties of merchantability and fitness for a particular purpose. 
  459. ;;; 
  460. ;;; In no event shall Graphic Systems, Inc. be liable to anyone for special, 
  461. ;;; collateral, incidental, or consequential damages in connection with or 
  462. ;;; arising out of purchase or use of these materials.  The entire risk as to
  463. ;;; the quality and performance of the program is with the user.  Should the 
  464. ;;; program prove defective, the user assumes the entire cost of all necessary 
  465. ;;; servicing, repair or correction. 
  466. ;;; 
  467. ;;; For condition of use and permission to use these materials for publication 
  468. ;;; in other than the English language, contact Graphic Systems, Inc. 
  469. ;;; 
  470. ;;; Graphic Systems, Inc. reserves the right to revise and improve its products 
  471. ;;; as it sees fit.  This publication describes the state of this product at 
  472. ;;; the time of its publication, and may not reflect the product at all times 
  473. ;;; in the future. 
  474.  
  475.